package org.mzy.basic.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.mzy.basic.anno.LogAnno;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Package: org.mzy.basic.aop
 * @ClassName: LogAop
 * @Date:2022/10/12 18:52
 * @Description:
 */

// 作用就是实现bean的注入
@Component
// 作用是把当前类标识为一个切面供容器读取
@Aspect
public class LogAop {
    @Pointcut("@annotation(org.mzy.basic.anno.LogAnno)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) {
        // 获取到所有的方法
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        LogAnno annotation = method.getAnnotation(LogAnno.class);
        String method1 = annotation.method();
        String methodDes = annotation.methodDes();
        // 执行业务方法
        Object proceed = null;
        try {
            proceed = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        // 添加日志
        return proceed;
    }
}
